package com.hsav.kcxb.main;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.hsav.kcxb.activity.PlayActivity;
import com.hsav.kcxb.activity.RecordActivity;
import com.kcbsdk.MyRankBa.AObbligatoList;
import com.kcbsdk.cjmkcs;
import com.kcbsdk.wpapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import main.MREG;
import main.MyDevice;
import main.MyDomain;
import main.MyManage;
import main.MyRankBa;
import main.MyTools;

public class MSYS implements wpapper {
    public static MyDevice mMyDevice;
    public static MyTools mMyTools;
    public static MyManage mMyManage;
    public static MyDomain mMyDomain;
    public static MyRankBa mMyRankBa;

    public static List<Activity> mActivitys = new ArrayList<>();
    private static AObbligatoList mAObbligatoList;

    public static void SetStartUp(){
        MREG.g4LastSelect = -1;

        mMyTools = new MyTools();
        mMyManage = new MyManage();
        mMyDomain = new MyDomain();
        mMyDevice = new MyDevice();
        mMyRankBa = new MyRankBa();

        mMyTools.Log("MSYS SetStartUp");
        mMyTools.mSvrTools.g4Test_0x21 = 0x21;
        mMyRankBa.mSvrRankBa.g4Test_0x21 = 0x21;
        mMyManage.mSvrManage.g4Test_0x21 = 0x21;
        cjmkcs.systemSetDebugFlag(0x7f, 0);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                onTimer();
            }
        }, 1, 300);
    }

    public static void backgroundActivity(){
        mActivitys.clear();
    }
    public static void finishActivity(){
        for (int counter = 0; counter < mActivitys.size(); counter++){
            mActivitys.get(counter).finish();
        }
    }

    public static void addActivity(Activity activity){
        mActivitys.add(activity);
    }
    public static void removeActivity(Activity activity){
        mActivitys.remove(activity);
        onDestroy();
    }
    public static void onDestroy(){
        //MSTRING(String.format("g4LastSelect %08X", MREG.g4LastSelect));
        mMyTools.mSvrTools.g4EventMethod |= TOOLS_EVENT_ON_DESTROY;
        mMyManage.mSvrManage.g4EventMethod |= MANAGE_EVENT_ON_DESTROY;
        mMyDomain.mSvrDomain.g4EventMethod |= DOMAIN_EVENT_ON_DESTROY;
        mMyDevice.mSvrDevice.g4EventMethod |= DEVICE_EVENT_ON_DESTROY;
        mMyRankBa.mSvrRankBa.g4EventMethod |= RANKBA_EVENT_ON_DESTROY;
        onTimer();
        mMyTools.mSvrTools.g4EventMethod &= ~TOOLS_EVENT_ON_DESTROY;
        mMyManage.mSvrManage.g4EventMethod &= ~MANAGE_EVENT_ON_DESTROY;
        mMyDomain.mSvrDomain.g4EventMethod &= ~DOMAIN_EVENT_ON_DESTROY;
        mMyDevice.mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_ON_DESTROY;
        mMyRankBa.mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_ON_DESTROY;
    }
    public static void onResume(){
        mMyTools.mSvrTools.g4EventMethod |= TOOLS_EVENT_ON_RESUME;
        mMyManage.mSvrManage.g4EventMethod |= MANAGE_EVENT_ON_RESUME;
        mMyDomain.mSvrDomain.g4EventMethod |= DOMAIN_EVENT_ON_RESUME;
        mMyDevice.mSvrDevice.g4EventMethod |= DEVICE_EVENT_ON_RESUME;
        mMyRankBa.mSvrRankBa.g4EventMethod |= RANKBA_EVENT_ON_RESUME;
        onTimer();
        mMyTools.mSvrTools.g4EventMethod &= ~TOOLS_EVENT_ON_RESUME;
        mMyManage.mSvrManage.g4EventMethod &= ~MANAGE_EVENT_ON_RESUME;
        mMyDomain.mSvrDomain.g4EventMethod &= ~DOMAIN_EVENT_ON_RESUME;
        mMyDevice.mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_ON_RESUME;
        mMyRankBa.mSvrRankBa.g4EventMethod &= ~RANKBA_EVENT_ON_RESUME;
    }

    public static void onTimer(){
        if (mMyTools.mActivity != null && mMyTools.mEventHandler != null) {
            mMyTools.mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mMyTools.pollingTimer();
                }
            });
        }
        if (mMyManage.mActivity != null && mMyManage.mEventHandler != null) {
            mMyManage.mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mMyManage.pollingTimer();
                }
            });
        }
        if (mMyDomain.mActivity != null && mMyDomain.mEventHandler != null) {
            mMyDomain.mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mMyDomain.pollingTimer();
                }
            });
        }
        if (mMyDevice.mActivity != null && mMyDevice.mEventHandler != null) {
            mMyDevice.mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mMyDevice.pollingTimer();
                }
            });
        }
        if (mMyRankBa.mActivity != null && mMyRankBa.mEventHandler != null) {
            mMyRankBa.mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mMyRankBa.pollingTimer();
                }
            });
        }
    }
    public static String getWorkingPath(){
        return cjmkcs.systemGetWpapperString(wpTOOLS_WorkingPath);
    }
    public static String getDownloadPath(){
        return cjmkcs.systemGetWpapperString(wpTOOLS_DownloadPath);
    }

    public static Intent startPlayActivity(int userID, int songID, boolean isVideo, Context context){
        Intent intent = new Intent(context, PlayActivity.class);
        intent.putExtra("userID", userID);
        intent.putExtra("songID", songID);
        intent.putExtra("isVideo", isVideo);
        return intent;
//        startActivity(intent);
    }
    public static Intent startRecordActivity(Context context, AObbligatoList aObbligatoList){
        Intent intent = new Intent(context, RecordActivity.class);
        mAObbligatoList = aObbligatoList;
        return intent;
//        startActivity(intent);
    }
    public static AObbligatoList getAObbligatoList(){
        AObbligatoList aObbligatoList = mAObbligatoList;
        mAObbligatoList = null;
        return aObbligatoList;
    }
    public static void MSTRING(String string) {
        Log.d("MSYS", string);
        //      System.out.println(x);
    }
}
